<?php

namespace Modules\Agent\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Agent\Entities\Brokerage;

use Validator;
use ThrowException;
use Auth;

class BrokerageController extends Controller
{
    public function detail(Request $req)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        return [
            'withdrawable' => $user->asset('redpack')->balance,
            'pending' => [
                'money' => $user->brokerages()->status('pending')->sum('money'),
            ],
            'all' => [
                'money' => $user->brokerages()->sum('money'),
            ]
        ];
    }


    public function index(Request $req)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $options = $req->all();

        if ($req->input('user_id')) {
            $options['where']['order_user_id'] = $req->user_id;
        }

        $options['where']['user_id'] = $user->id;

        $options['with'] = ['order_user', 'order_sku'];

        $options['only'] = [
            'id',
            'uuid',
            'order_number',
            'money',
            'score',
            'invite_level',
            'order_type',
            'completed_at',
            'union_status',
            'union_status_text',
            'created_at',
            'order_user' => [
                'id',
                'uuid',
                'name',
                'headimg'
            ],
            'order_sku' => [
                'id',
                'uuid',
                'title',
                'thumb',
                'number',
                'discount_money_price',
                'discount_score_price',
                'total'
            ]
        ];


        $list = Brokerage::getList($options);

        return $list->generateListResponse();
    }

}
